/**
 * 
 */
package com.sip.scanadapter.domainparsers;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sip.dbadapter.dao.domain.ConsumeType;
import com.sun.research.ws.wadl.Application;
import com.sun.research.ws.wadl.Param;
import com.sun.research.ws.wadl.Resource;
import com.sun.research.ws.wadl.Resources;

/**
 * @author Dhanushka Ariyasingha.
 * 
 */
public class ConsumerTypeParser implements Parser<Map<String, Set<ConsumeType>>, Application> {

	private Map<String, Set<ConsumeType>> consumerTypeObjCollection = null;

	private StringBuilder urlBuilderServicePath = new StringBuilder();

	private StringBuilder urlBuilderServiceMethod = new StringBuilder();

	public ConsumerTypeParser() {
		consumerTypeObjCollection = new HashMap<String, Set<ConsumeType>>();
	}


	public Map<String, Set<ConsumeType>> parser(Application application) {

		final List<Resources> resourceList = application.getResources();
		collectServiceComponent(resourceList);

		return consumerTypeObjCollection;
	}

	private void collectServiceComponent(final List<Resources> resourceList) {

		for (final Resources resources : resourceList) {

			String baseUrl = resources.getBase();
			System.out.println("BaseUrl:" + baseUrl);

			final List<Resource> subResourceList = resources.getResource();

			collectServiceInfo(subResourceList);
		}

	}

	private void collectServiceInfo(final List<Resource> subResourceList) {

		for (final Resource resource : subResourceList) {

			final String resourcePath = resource.getPath();
			System.out.println("Service Path:" + resourcePath);

			urlBuilderServicePath.append(resourcePath);

			collectServiceMethodInfo(resource);

			urlBuilderServicePath.delete(0, urlBuilderServicePath.length());
		}
	}

	private void collectServiceMethodInfo(final Resource resource) {

		final List<Object> methodOrResourceList1 = resource.getMethodOrResource();

		for (Object object : methodOrResourceList1) {

			if (object instanceof Resource) {

				final Resource serviceResource = (Resource) object;

				collectServiceMethodDefinitionInfo(serviceResource);
			}
		}
	}

	private void collectServiceMethodDefinitionInfo(final Resource serviceResource) {

		final List<Param> paramterList = serviceResource.getParam();
		System.out.println("Service Method:" + serviceResource.getPath());

		String serviceMethod = serviceResource.getPath();
		serviceMethod = serviceMethod.substring(0, 1).equals("/") ? serviceMethod : "/" + serviceMethod;

		urlBuilderServiceMethod.append(serviceMethod);
		
		final Set<ConsumeType> consumerTypeDomainList =new HashSet<ConsumeType>();

		for (final Param param : paramterList) {

			System.out.println(" Parameters Name:" + param.getName());
			System.out.println(" Parameters Type:" + param.getType());
			
			final ConsumeType consumerType =new ConsumeType();
			final String grammerDefinition = param.getType() == null ? "NULL" : param.getType().toString();
			
			consumerType.setName(param.getName());
			consumerType.setGrammerDefinition(grammerDefinition);
			
			consumerTypeDomainList.add(consumerType);
		}

		consumerTypeObjCollection.put(urlBuilderServicePath.toString() + urlBuilderServiceMethod.toString(),
				consumerTypeDomainList);
		urlBuilderServiceMethod.delete(0, urlBuilderServiceMethod.length());
	}

}
